<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item prop="projectName" label="项目名称" size="small">
            <el-input v-model="searchForm.projectName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()"
                 v-if="roleType===0 || roleType===1 || roleType===2 || roleType===10 || roleType===13 ">添加
      </el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="1000px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="110px">
          <el-form-item prop="registrationTime" label="日期" size="small">
            <el-date-picker
              v-model="teamRow.registrationTime"
              type="date"
              class="dataclass"
              value-format="timestamp"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="工程号" size="small">
            <el-input v-model="teamRow.projectCode" style="width: 460px"></el-input>
          </el-form-item>
          <el-form-item prop="projectName" label="项目名称" size="small">
            <el-input v-model="teamRow.projectName" style="width: 460px"></el-input>
          </el-form-item>
          <el-form-item label="用章人" size="small">
            <el-input v-model="teamRow.useSeal" style="width: 460px"></el-input>
          </el-form-item>

          <el-row v-for="(item,index) in drawStageList">
            <el-form-item :label="`图纸${index+1}`" size="small">
            </el-form-item>
            <el-col class="other">
              <el-form-item label="专业" size="small">
                <el-input v-model="item.profession"></el-input>
              </el-form-item>
              <el-form-item prop="projectResponsible" label="项目负责" size="small">
                <el-input v-model="item.projectResponsible"></el-input>
              </el-form-item>
              <el-form-item prop="design" label="设计" size="small">
                <el-input v-model="item.design"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="other">
              <el-form-item prop="validation" label="审定" size="small">
                <el-input v-model="item.validation"></el-input>
              </el-form-item>
              <el-form-item prop="professionResponsible" label="专业负责" size="small">
                <el-input v-model="item.professionResponsible"></el-input>
              </el-form-item>
              <el-form-item prop="mapping" label="制图" size="small">
                <el-input v-model="item.mapping"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="other">
              <el-form-item label="审核" size="small">
                <el-input v-model="item.review"></el-input>
              </el-form-item>
              <el-form-item prop="proofread" label="校对" size="small">
                <el-input v-model="item.proofread"></el-input>
              </el-form-item>
              <el-form-item label="图纸备注" size="small">
                <el-input type="text" v-model="item.remark"></el-input>
              </el-form-item>
            </el-col>
            <!--
            <el-form-item label="制图阶段" size="small">
              <el-select v-model="teamRow.drawingStage" placeholder="请选择">
                <el-option
                  v-for="item in global.stageList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="signature" label="电子版签字" size="small">
              <el-input v-model="teamRow.signature"></el-input>
            </el-form-item>
            -->
          </el-row>

          <el-row>
            <el-form-item label="文本" size="small">
            </el-form-item>
            <el-col class="other">
              <el-form-item label="文本数量" size="small">
                <el-input type="number" v-model.number="teamRow.count"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="other">
              <el-form-item label="文本备注" size="small">
                <el-input v-model="teamRow.textRemark"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading">
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="80"
        v-if="roleType===0 || roleType===1 || roleType===2 || roleType===10 || roleType===13 "
      >
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.drawId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    addRegistration,
    deleteRegistration,
    getList,
    selectProjectName,
    selectRegistration,
    updateRegistration
  } from '@/api/blueprints'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const registrationTime = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const profession = function(row, column) {
    if (row.profession) {
      let obj = {}
      obj = global.professionList.find((item) => {
        return item.value === parseInt(row.profession) // 筛选出匹配数据
      })
      return obj.label
    }
  }

  const drawingStage = function(row, column) {
    if (row.drawingStage) {
      let obj = {}
      obj = global.stageList.find((item) => {
        return item.value === row.drawingStage // 筛选出匹配数据
      })
      return obj.label
    }
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'registrationTime', label: '日期', width: '100', component: registrationTime },
          { prop: 'projectCode', label: '工程号', minWidth: '10%' },
          { prop: 'projectName', label: '项目名称', minWidth: '30%' },
          { prop: 'useSeal', label: '用章人', minWidth: '10%' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        professions: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增图纸文本登记',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        pca: pca,
        rules: {
          registrationTime: [
            { required: true, message: '请选择日期', trigger: 'change' }
          ],
          projectCode: [
            { required: true, message: '请输入工程编号', trigger: 'change' }
          ],
          projectName: [
            { required: true, message: '请输入项目名称', trigger: 'change' }
          ],
          profession: [
            { required: true, message: '请选择专业', trigger: 'change' }
          ]
        },
        visible: false,
        drawStageList: []
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      },
      roleType() {
        return this.$store.state.user.roleType
      },
      teamId() {
        return this.$store.state.user.teamId
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          console.log(response)
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增图纸文本登记'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.drawStageList = []
        for (let i = 1; i < 7; i++) {
          this.drawStageList.push({
            profession: '',
            professionResponsible: '',
            validation: '',
            proofread: '',
            review: '',
            design: '',
            projectResponsible: '',
            mapping: '',
            remark: ''
          })
        }
        this.dialogType = 1
        this.$set(this.teamRow, 'teamId', this.teamId)
      },
      editTeam(drawId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改图纸文本登记'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.drawStageList = []
        this.dialogType = 0
        const params = {
          drawId: drawId
        }
        selectRegistration(params).then(response => {
          this.teamRow = response.responseBody
          this.drawStageList = this.teamRow.drawStageList
        })
      },
      submitForm(formName) {
        const params = {
          teamId: this.teamRow.teamId,
          registrationTime: this.teamRow.registrationTime,
          projectCode: this.teamRow.projectCode,
          projectName: this.teamRow.projectName,
          useSeal: this.teamRow.useSeal || '',
          count: this.teamRow.count || 0,
          textRemark: this.teamRow.textRemark || '',
          drawStageList: this.drawStageList
        }
        if (!this.dialogType) {
          params.drawId = this.teamRow.drawId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addRegistration(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateRegistration(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        console.log(this.$refs[`popover-${scope.$index}`])
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          drawId: scope.row.drawId,
          isDel: 1
        }
        deleteRegistration(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      addressChange() {
        this.teamRow.provinceId = this.teamRow.addressId[0]
        this.teamRow.cityId = this.teamRow.addressId[1]
        this.teamRow.address = this.pca[86][this.teamRow.addressId[0]] + this.pca[this.teamRow.addressId[0]][this.teamRow.addressId[1]]
      },
      querySearchProject(queryString, callback) {
        // queryString 为在框中输入的值
        // callback 回调函数,将处理好的数据推回
        delete this.teamRow.projectId
        if (queryString) {
          const params = {
            contractCode: queryString
          }
          selectProjectName(params).then(response => {
            for (const i of response.responseBody) {
              i.value = i.contractCode
            }
            let projectList = response.responseBody
            if (projectList.length === 0) {
              projectList = [{ value: '暂无数据' }]
            }
            callback(projectList)
          })
        } else {
          callback([])
        }
      },
      handleSelectProject(item) {
        this.$set(this.teamRow, 'projectId', item.projectId)
        this.$set(this.teamRow, 'projectName', item.projectName)
        this.$set(this.teamRow, 'teamId', item.teamId)
        this.$set(this.teamRow, 'teamName', item.teamName)
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .textareaclass {
    width: 460px;
    height: 200px;
  }

  .selectclass {
    width: 460px;
  }

  .dataclass {
    width: 160px;
  }

  .other {
    width: 300px;
  }

  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
